
(function($){
	
	var validateTags = ["input","select","textarea"];
	
	//将string转换成标准JSON字符串
	var convertToJsonStringFun = function(){
		var vVal = arguments[0];
		if(vVal==null || vVal=="") return;
		
		if(vVal.indexOf(",")>=0){
			var keyValArray = vVal.split(",");
			for(var i=0;i<keyValArray.length;i++){
				var kvArr = keyValArray[i].split(":");
				kvArr[0] = "\""+kvArr[0]+"\"";
				kvArr[1] = "\""+kvArr[1]+"\"";
				keyValArray[i] = kvArr[0]+":"+kvArr[1];
			}
			return "{"+keyValArray.toString()+"}";
		}else{
			keyValArray  = vVal;
			var kvArr = keyValArray.split(":");
			kvArr[0] = "\""+kvArr[0]+"\"";
			kvArr[1] = "\""+kvArr[1]+"\"";
			return "{"+kvArr[0]+":"+kvArr[1]+"}";
		}
		
		return ;
	} ;
	
	var validateFuns = {
		
		//必填
		required : function(){
			var flag = true;
			var jsonObj = arguments[0];
			var validateVal = $.trim($(this).val());
			if(validateVal == "" || validateVal == null){
				flag = false;
				$(this).parent("div").addClass("has-error");
//				$(this).next(".help-block").remove();
//				$(this).after("<span class=\"help-block\" style=\"color:red\">"+jsonObj.required+"</span>");
				
				$(this).parent("div").children(".help-block").remove();
				$(this).parent("div").append("<span class=\"help-block\" style=\"color:red\">"+jsonObj.required+"</span>");
			}else{
				$(this).parent("div").children(".help-block").remove();
			}
			
			return flag;
		},
		
		//最大长度
		maxLength : function(){
			var flag = true;
			var jsonObj = arguments[0];
			var maxLength = jsonObj.maxLength;
			if(isNaN(Number(maxLength))){
				alert("maxLength必须为数字！");
				return false;
			}
			var validateVal = $(this).val();
			if(validateVal.length>Number(maxLength)){
				flag = false;
				$(this).parent("div").addClass("has-error");
				$(this).next(".help-block").remove();
				$(this).after("<span class=\"help-block\" style=\"color:red\">最长只能输入"+maxLength+"个字符</span>");
			}
			
			return flag;
		}
			
	};
	
	$("form").bind("submit",function(){
		
		var validateFlag = true;
		
		try{
			
			var formChildren = $(this).find("*");
			for(var i=0;i<formChildren.length;i++){
				var childDom = formChildren[i];
				if(childDom!=null){
					var childTagName = childDom.tagName;
					
					childTagName = childTagName.toLowerCase();		//转小写
					var isExists = $.inArray(childTagName, validateTags);  //返回 下标
					
					if(isExists>=0){
						
						var fVal = $(childDom).attr("forvalid");
						if(fVal!=null && fVal!=""){
							var json_fVal  = fVal.substring(1,fVal.length-1);
							var jsonString = convertToJsonStringFun(json_fVal);
							var jsonObj = $.parseJSON(jsonString);
							for(objKey in jsonObj){
								var returnFlag = true;
								for(vdfKey in validateFuns){
									if(objKey == vdfKey){	//如果相等，说明需要验证
										returnFlag = validateFuns[vdfKey].apply(childDom, [jsonObj]) ;
									}
								}
								
								if(!returnFlag){
									validateFlag = returnFlag;
								}
							}
//							validateFuns.required(childDom);
						}
						
					}
				}
			}
			
		}catch(e){
			validateFlag = false;
		}
		
		return validateFlag;
	});
})(jQuery);